package exceptionHandler;


import java.io.File;
import java.io.IOException;

/**
 * @Author forsake
 * @Description
 * throw相当于return，
 * 异常执行顺序：try代码-》catch代码-》finally代码-》finally的return，catch的return
 * @Date 18:36 2022/11/28
 **/

public class ExceptionHandler {
    public static void test() {
        User user = new User();
        System.out.println(user.getAge().byteValue());
        // 不对异常进行处理，出现运行时异常程序终止
        System.out.println("运行时异常程序终止吗");
    }
    public static void test1() {
        User user = new User();
        try {
            System.out.println(user.getAge().byteValue());
        } catch (Exception e) {
            e.printStackTrace();
        }
        // try-catch后程序不会终止
        System.out.println("运行时异常程序终止吗");
    }
    public static void test2() {
        User user = new User();
        try {
            System.out.println(user.getAge().byteValue());
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
        // catch块中存在return程序终止
        System.out.println("运行时异常程序终止吗");
    }
    public static void test3() {
        File file = new File("a.txt");
        if(!file.exists()){
            // IOException、SQLException异常以及用户自定义异常不进行try，catch则不允许编译。
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public static void test4() {
        User user = new User();
        user.setName("方庆");
        System.out.println(user.getName());
        // 手动抛出异常程序直接终止
        throw new BadException("出现重大错误");
        // 直接编译错误，代码块并不能到达此处
//        System.out.println("直接抛出异常程序会终止吗");
    }
    public static void test5() {
        try {
            int i = 1 / 0;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BadException("出错");
        } finally {
            // 会执行
            System.out.println("catch抛出异常finally块还会执行吗");
        }
    }
    public static void test6() {
        try {
            int i = 1 / 0;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BadException("出错");
        } finally {
            // finally块执行return，则catch块中的throw不会执行，所以throw相当于return。
            System.out.println("catch抛出异常finally块还会执行吗");
            return;
        }
    }
    public static void test7() {
        try {
            System.out.println("抛出异常前");
            int i = 1 / 0;
            System.out.println("抛出异常后");
            return;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("进入catch块");
        } finally {
            System.out.println("进入finally块");
        }
        System.out.println("走出异常");
    }
    public static void test8() {
        int[] intArray = new int[3];
        try {
            for (int i = 0; i <= intArray.length; i++) {
                intArray[i] = i;
                System.out.println("intArray[" + i + "] = " + intArray[i]);
                System.out.println("intArray[" + i + "]模 " + (i - 2) + "的值:  "
                        + intArray[i] % (i - 2));
            }
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("intArray数组下标越界异常。");
        } catch (ArithmeticException e) {
            System.out.println("除数为0异常。");
        }
        System.out.println("程序正常结束。");
    }

}


